from datetime import datetime

from django.db import models


class GoodsCategory(models.Model):
    """商品类别"""

    CATEGORY_TYPE= (
      (1, "一级类目"),
      (2, "二级类目"),
      (3, "三级类目")
    )

    name = models.CharField(max_length=30, default="", verbose_name="类别名", help_text="类别名")
    code = models.CharField(max_length=30, default="", verbose_name="类别编码", help_text="类别编码")
    desc = models.TextField(max_length=300, default="", verbose_name="类别描述", help_text="类别描述")
    category_type = models.CharField(max_length=100,choices=CATEGORY_TYPE, verbose_name="类目级别", help_text="类目级别")
    parent_category = models.ForeignKey("self", null=True, blank=True, verbose_name="父泪目级别", help_text="父泪目级别", related_name="sub_cat", on_delete=models.DO_NOTHING,)
    is_tab = models.BooleanField(default=False, verbose_name="是否导航", help_text="是否导航")

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间", help_text="添加时间")

    class Meta:
        verbose_name = "GoodsCategory"
        verbose_name_plural = "GoodsCategorys"

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("GoodsCategory_detail", kwargs={"pk": self.pk})

class GoodsCategoryBrand(models.Model):

    name = models.CharField(max_length=30, default="", verbose_name="品牌名", help_text="品牌名")
    desc = models.CharField(max_length=300, default="", verbose_name="品牌描述", help_text="品牌描述")
    image = models.ImageField(max_length=200, upload_to="brands/")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "GoodsCategoryBrand"
        verbose_name_plural = "GoodsCategoryBrands"

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("GoodsCategoryBrand_detail", kwargs={"pk": self.pk})

class Goods(models.Model):
    """商品"""

    category = models.ForeignKey(GoodsCategory, null=True, blank=True,on_delete=models.DO_NOTHING,)
    goods_sn = models.CharField(max_length=20)
    name = models.CharField(max_length=100)
    click_num = models.IntegerField(default=0)
    sold_num = models.IntegerField(default=0)
    fav_num = models.IntegerField(default=0)
    goods_num =  models.IntegerField(default=0)
    market_price = models.FloatField( null=True, blank=True,)
    goods_brief = models.TextField( null=True, blank=True,)
    goods_desc = models.TextField( null=True, blank=True,)
    ship_free = models.BooleanField(default=False)
    goods_front_image = models.ImageField(max_length=200, upload_to="brands/")
    is_new = models.BooleanField(default=False)
    is_hot = models.BooleanField(default=False)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "Goods"
        verbose_name_plural = ("Goodss")

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("Goods_detail", kwargs={"pk": self.pk})

class GoodsImages(models.Model):
    """商品轮播图"""

    goods = models.ForeignKey(Goods, on_delete=models.DO_NOTHING,)
    image = models.ImageField(max_length=200, upload_to="brands/")
    image_url = models.CharField(max_length=100)
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = ("GoodsImages")
        verbose_name_plural = ("GoodsImagess")

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse("GoodsImages_detail", kwargs={"pk": self.pk})
